package com.tronapi.c.service;

import com.tronapi.c.dao.DeviceMobileDao;
import com.tronapi.c.model.d.DeviceMobile;
import com.tronapi.c.model.request.RegisterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DeviceService {

    @Autowired
    private DeviceMobileDao deviceMobileDao;

    /**
     * 注册或登录 设备
     *
     * @param request
     */
    public void loginRegister(RegisterRequest request) {
        DeviceMobile deviceMobile = new DeviceMobile();
        deviceMobile.setDeviceId(request.getDeviceId());
        deviceMobile.setAndroidVersion(request.getVersion());
        deviceMobile.setAppName(request.getAppName());
        deviceMobile.setRegisterTime(new Date());
        deviceMobile.setLastLoginTime(new Date());
        DeviceMobile device = deviceMobileDao.findByDeviceId(request.getDeviceId(), request.getAppName());
        if (device == null) {
            deviceMobileDao.insert(deviceMobile);
        } else {
            device.setLastLoginTime(new Date());
            deviceMobileDao.updateById(device);
        }
    }
}
